home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / tests / misc / loadscanobjs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  7.0 KB  |  288 lines

  1. /*
  2.  * $RCSfile: loadscanobjs.c,v $
  3.  * $Revision: 1.1.1.1 $
  4.  * $Date: 1996/05/04 21:56:07 $
  5.  *    This example program loads a file with objects and
  6.  *    then scans the file.  The program takes one parameter, the
  7.  *    number of objects to create.
  8.  */
  9.  
  10.  
  11. #include "sm_client.h"
  12. #include <stdlib.h>
  13.  
  14. #ifndef __cplusplus
  15.     extern char        *getenv(char*);
  16. #endif
  17.  
  18.  
  19. /* 
  20.  * 'clientvolume' is the name of the file containing the client's
  21.  *  disk volume (created with the 'formatvol' utility).
  22.  */
  23. #define  VOL    getenv("EVOLID") 
  24. VOLID    volid;  /* volume ID */
  25.  
  26. /*
  27.  *    Store the buffer group and transaction information in
  28.  *    these global variables.
  29.  */
  30. int        bufGroup;     /* buffer group index */
  31. TID        tid;          /* transaction ID */
  32.  
  33. /*
  34.  * Each dummy object consists of only an integer-valued field.
  35.  */
  36. typedef struct { 
  37.     int          value;        /* the object's value */
  38. } DUMMYOBJ;
  39.  
  40.  
  41. /*
  42.  *    Declare the error handling function
  43.  */
  44. int ErrorCheck (int, char *);
  45.  
  46.  
  47. /* 
  48.  * This example routine creates a file and then creates 100 dummy data 
  49.  * objects in the file. The ID of the file is stored as a root entry
  50.  * in the volume and is used by the routine Scan( )
  51.  * to determine which file to scan.
  52.  */
  53. void
  54. Load (int numObjs) {
  55.     int        e;            /* error return */
  56.     int        i;            /* loop index */
  57.     FID        fid;          /* file ID */
  58.     OID        oid;          /* object ID */
  59.     OBJHDR     objHdr;       /* object header */
  60.     DUMMYOBJ   dummyObj;     /* dummy object */
  61.  
  62.     /*
  63.      * Create the file.
  64.      */
  65.     e = sm_CreateFile(bufGroup, volid, &fid);
  66.     ErrorCheck(e, "sm_CreateFile");
  67.     
  68.     /*
  69.      * Fill the file with 100 objects.
  70.      */
  71.     for (i = 0; i < numObjs; i++) {
  72.         dummyObj.value = i;
  73.         e = sm_CreateObject(bufGroup, &fid, NEAR_LAST, NULL,
  74.                 &objHdr, sizeof(DUMMYOBJ), (char*) &dummyObj, &oid);
  75.         ErrorCheck(e, "sm_CreateObject");
  76.     }
  77.  
  78.     /*
  79.      * Remember the file's ID in a root entry so Scan( )
  80.      * can find it.
  81.      */
  82.     e = sm_SetRootEntry(volid, "fid", (char*)&fid, sizeof(FID));
  83.     ErrorCheck(e, "sm_SetRootEntry");
  84. }
  85.  
  86.  
  87. /* 
  88.  * This routine scans and prints the file created with 
  89.  * the Load( ) routine.
  90.  */
  91.  void
  92. Scan ( )
  93. {
  94.     int        e;             /* error return */
  95.     int        i;             /* index */
  96.     FID        fid;           /* file ID */
  97.     BOOL       eof;           /* end of file flag */
  98.     USERDESC   *userDesc;     /* user descriptor */
  99.     DUMMYOBJ   **dummyObj;    /* pointer to dummy object */
  100.     int        rootDataSize;  /* size of fid from root entry */
  101.     SCANDESC   *scanDesc;      /* scan descriptor */    
  102.  
  103.     /*
  104.      * Get the file's ID from the root page entry.
  105.      */
  106.     rootDataSize = sizeof(fid);
  107.     e = sm_GetRootEntry(volid, "fid", &fid, &rootDataSize);
  108.     ErrorCheck(e, "sm_GetRootEntry");
  109.     if (rootDataSize != sizeof(fid)) {
  110.         fprintf(stderr, "Error: Bad root entry\n");
  111.         exit(1);
  112.     }
  113.  
  114.     /*
  115.      *    Start-up the scan 
  116.      */
  117.     e = sm_OpenScan(&fid, 0, 10/*page buf group*/, &scanDesc, NULL);
  118.     ErrorCheck(e, "sm_OpenScan");
  119.     e = sm_ScanNextObject(scanDesc, 0, READ_ALL, &userDesc, &eof);
  120.     ErrorCheck(e, "sm_ScanNextObject");
  121.  
  122.     for (i = 0; eof == FALSE; i++) {
  123.  
  124.         /* 
  125.          * Cast an object pointer onto the basePtr of
  126.          * the user descriptor.
  127.          *
  128.          * CAUTION: Double indirection via a user descriptor
  129.          * should always be used to access objects in the 
  130.          * buffer pool! The storage manager may move objects 
  131.          * in the buffer pool, and therefore double indirection 
  132.          * is always necessary.
  133.          */
  134.         dummyObj = (DUMMYOBJ **) &userDesc->basePtr;
  135.  
  136.         /*
  137.          * Print out the object's value
  138.          */
  139.         printf("object[%d].value = %d\n", i, (*dummyObj)->value);
  140.  
  141.         /*
  142.          * Get the next object.
  143.          */
  144.         e = sm_ScanNextObject(scanDesc, 0, READ_ALL, &userDesc, &eof);
  145.         ErrorCheck(e, "sm_ScanNextObject");
  146.     }
  147. }
  148.  
  149.  
  150. /*
  151.  * ErrorCheck( ) is a simple routine for checking
  152.  * Storage Manager return codes.
  153.  */
  154.  int
  155. ErrorCheck (
  156.     int    e,              /* error return code */
  157.     char   *routine        /* the routine that caused an error */
  158. )
  159. {
  160.     if (e < 0) {
  161.         fprintf(stderr, "Got a Storage Manager error from %s\n", routine);
  162.         fprintf(stderr, "Error = %s\n", sm_Error(sm_errno));
  163.         if (sm_errno == esmTRANSABORTED) {
  164.             fprintf(stderr, "Transaction  aborted by server due to: %s\n", sm_Error(sm_reason));
  165.         }
  166.         exit(1);
  167.     }
  168.     return(0);
  169. }
  170.  
  171.  
  172. /*
  173.  *    Main program:
  174.  */
  175. main(int argc, char** argv)
  176. {
  177.  
  178.     int        objCount;
  179.     int        e;
  180.     FILE    *errorStream;
  181.     char    *errorMsg;
  182.     int        rootDataSize;  /* size of fid from root entry */
  183.     FID        fid;
  184.  
  185.     /*
  186.      *  Read the default configuration files to have the bufpages
  187.      *  option set.
  188.      */
  189.     e = sm_ReadConfigFile(NULL, argv[0], &errorMsg);
  190.     if (e != esmNOERROR) {
  191.         fprintf(stderr, "Configuration file error: %s\n", errorMsg);
  192.         ErrorCheck(e, "sm_ReadConfigFile");
  193.         exit(0);
  194.     }
  195.  
  196.     /*
  197.      *    Set any options from the command line
  198.      */
  199.     e = sm_ParseCommandLine(&argc, argv, &errorMsg);
  200.     if (e != esmNOERROR) {
  201.         fprintf(stderr, "command line error: %s\n", errorMsg);
  202.         ErrorCheck(e, "sm_PargeCommandLine");
  203.         exit(0);
  204.     }
  205.  
  206.     /* check for the proper number of arguments */
  207.     if (argc !=2 ) {
  208.         fprintf(stderr,"usage: %s count\n", argv[0]);
  209.         exit(-1);
  210.     }
  211.     /* get the parameters of the test */
  212.     objCount = atoi(argv[1]);
  213.  
  214.     /*
  215.      * Initialize the storage manager.
  216.      */
  217.     e = sm_Initialize();
  218.     ErrorCheck(e, "sm_Initialize");
  219.     tid = 0;
  220.  
  221.     /*
  222.      *    Send all error output to /dev/null since any "real errors" 
  223.      *    will be seen in an sm_* call.
  224.      */
  225.     errorStream = fopen("/dev/null", "a");
  226.     if (errorStream == 0) {
  227.         perror("Cannot open /dev/null");
  228.     }
  229.     sm_ErrorStream = errorStream;
  230.  
  231.     /*
  232.      * Get the ID of the volume to use
  233.      */
  234.     if (VOL == NULL) {
  235.         fprintf(stderr, "Error: you must setenv EVOLID.\n");
  236.         exit(1);
  237.     }
  238.     volid = atoi(VOL);
  239.  
  240.     /*
  241.      * Open an LRU buffer group of size 100.
  242.      */
  243.     e = sm_OpenBufferGroup(100, BF_LRU, &bufGroup, NOFLAGS);
  244.     ErrorCheck(e, "sm_OpenBufferGroup");
  245.     
  246.     /*
  247.      * Begin the transaction.
  248.      */
  249.     e = sm_BeginTransaction(&tid);
  250.     ErrorCheck(e, "sm_BeginTransaction");
  251.  
  252.     /*
  253.      * If a file exists from a previous Load() call, then destroy it.
  254.      */
  255.     rootDataSize = sizeof(fid);
  256.     e = sm_GetRootEntry(volid, "fid", &fid, &rootDataSize);
  257.     if (e == esmNOERROR) {
  258.         if (rootDataSize != sizeof(fid)) {
  259.             fprintf(stderr, "Error: Bad root entry\n");
  260.             exit(1);
  261.         }
  262.         e = sm_DestroyFile(bufGroup, &fid);
  263.         ErrorCheck(e, "sm_DestroyFile");
  264.     } else if (sm_errno == esmBADROOTNAME) {
  265.         /* no previous file */
  266.     } else {
  267.         ErrorCheck(e, "sm_GetRootEntry");
  268.     }
  269.  
  270.     /*
  271.      *    Load some objects and scan them
  272.      */
  273.     printf("Loading %d objects... \n", objCount);
  274.     Load(objCount);
  275.     printf("Scanning %d objects... \n", objCount);
  276.     Scan();
  277.  
  278.     e = sm_CommitTransaction(tid);
  279.     ErrorCheck(e, "sm_EndTransaction");
  280.  
  281.     e = sm_CloseBufferGroup(bufGroup);
  282.     ErrorCheck(e, "sm_CloseBufferGroup");
  283.     
  284.     e = sm_ShutDown( );
  285.     ErrorCheck(e, "sm_ShutDown");
  286. }
  287.  
  288.